package com.shundr.shipper.common.util;

import android.content.Context;
import android.widget.Toast;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationConfigeration;
import com.baidu.mapapi.map.MyLocationConfigeration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;

public class BaiduMapManager{
	private Context mContext;
	
	private BaiduMap mBaiduMap;
	
	private LocationMode mCurrentMode = LocationMode.NORMAL;
	private BitmapDescriptor mCurrentMarker;
	private LocationClient mLocClient;
	private GeoCoder mGeoCoder;
	
	private BDLocationListener locationListener;
	private OnGetGeoCoderResultListener geoCoderResultListener;
	
	public BaiduMapManager(Context context, BaiduMap baiduMap){
		this.mContext = context;
		this.mBaiduMap = baiduMap;
		this.mLocClient = new LocationClient(mContext);
	}
	
	public void initMap(){
		if(mBaiduMap != null){
			MapStatusUpdate ms = MapStatusUpdateFactory.zoomTo(14.0f); 
			mBaiduMap.setMapStatus(ms);//
			// 是否开启定位图层
			mBaiduMap.setMyLocationEnabled(true);
			mBaiduMap.setMyLocationConfigeration(new MyLocationConfigeration(mCurrentMode, true, mCurrentMarker));
		}
	}
	
	/**
	 * 设置缩放级别
	 * setMapStatus(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param level
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void setMapStatus(float level){
		try {
			MapStatusUpdate ms = MapStatusUpdateFactory.zoomTo(level); 
			mBaiduMap.setMapStatus(ms);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public LocationMode getCurrentMode(){
		return mBaiduMap.getLocationConfigeration().locationMode;
	}
	
	public void changeLocationMode(LocationMode mode){
		mCurrentMode = mode;
		mBaiduMap.setMyLocationConfigeration(new MyLocationConfigeration(mCurrentMode, true, mCurrentMarker));
	}
	
	public void changeType(int type){
		mBaiduMap.setMapType(type);
	}
	
	public BaiduMap getmBaiduMap() {
		return mBaiduMap;
	}
	
	public BitmapDescriptor getmCurrentMarker() {
		return mCurrentMarker;
	}

	public void setmCurrentMarker(int res) {
		this.mCurrentMarker = BitmapDescriptorFactory.fromResource(res);
		mBaiduMap.setMyLocationConfigeration(new MyLocationConfigeration(mCurrentMode, true, mCurrentMarker));
	}

	public void startLocation(){
		if(locationListener == null){
			Toast.makeText(mContext, "请初始化定位监听服务", Toast.LENGTH_LONG).show();
			return;
		}
		mLocClient.registerLocationListener(locationListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(999);
		option.setAddrType("all");
		mLocClient.setLocOption(option);
		mLocClient.start();
	}

	public void setLocationListener(BDLocationListener locationListener) {
		this.locationListener = locationListener;
	}
	
	public void setGeoCoderResultListener(
			OnGetGeoCoderResultListener geoCoderResultListener) {
		this.geoCoderResultListener = geoCoderResultListener;
	}

	public void resume(){
		if (mLocClient == null)
			return;
		if(!mLocClient.isStarted())
			mLocClient.start();
	}
	
	public void stop(){
		if (mLocClient == null)
			return;
		if(mLocClient.isStarted())
			mLocClient.stop();;
	}
	
	public void destroy() {
		if (mLocClient != null){
			if (locationListener != null){
				mLocClient.unRegisterLocationListener(locationListener);
			}
			mLocClient.stop();// 退出时销毁定位
		}
		// 关闭定位图层
		if (mBaiduMap != null){
			mBaiduMap.setMyLocationEnabled(false);
			mBaiduMap.clear();
		}
		if(mGeoCoder != null){
			mGeoCoder.destroy();
		}
	}
	
	/**
	 * 反编译地理位置信息
	 */
	public void reverseLocation(LatLng latLng){
		if(geoCoderResultListener == null){
			Toast.makeText(mContext, "请初始化反编译监听服务", Toast.LENGTH_LONG).show();
			return;
		}
		mGeoCoder = GeoCoder.newInstance();
		mGeoCoder.setOnGetGeoCodeResultListener(geoCoderResultListener);
		ReverseGeoCodeOption reverseOption = new ReverseGeoCodeOption();
//		reverseOption.location(LContext.mLatLng);
		reverseOption.location(latLng);
		mGeoCoder.reverseGeoCode(reverseOption);
	}
	
}
